/*
* @Author: 窦子滨<zibin_5257@163.com>
* @Date: 2019-05-08 9:40
 */
package main

import (
	zb "lanfengye/base/common"
	"lanfengye/base/etcd"
	hprose "github.com/lanfengye2008/hprose-golang/rpc"
	"net/http"
	"sync"
)

type Control interface {
	//通知方法映射文件修改
	MappingChange() *zb.ReturnData
	//通知手动配置微服务列表文件修改
	ManualChange() *zb.ReturnData
	//通知etcd server定义文件变动
	EtcdServersChange() *zb.ReturnData
	//方法映射增加映射
	MappingAdd(k string, v string) *zb.ReturnData
	//方法映射删除
	MappingDelete(k string) *zb.ReturnData
	//获取所有映射关系列表
	MappingAllList() *zb.ReturnData
	//获取映射关系
	MappingGet(k string) *zb.ReturnData
	//手动配置的微服务删除
	ManualDelete(key string) *zb.ReturnData
	//删除指定微服务的uri链接
	ManualDeleteUrl(key string, uri string) *zb.ReturnData
	//手动微服增加uri
	ManualAddUrl(key string, uri string) *zb.ReturnData
	//获取手动微服务的url列表
	ManualGet(key string) *zb.ReturnData
	//获取全部手动配置的微服务
	ManualAllList() *zb.ReturnData
	//手动微服务增加
	ManualAdd(key string, list []string) *zb.ReturnData
	//微服务管理器 获取所有数据
	ServicesAll() *zb.ReturnData
	//微服务管理器 删除微服务
	ServiceDelete(name string) *zb.ReturnData
	//服务对象 删除连接
	ServiceObjDeleteConn(name string, uri string) *zb.ReturnData
	ServiceObjConnCount(name string) *zb.ReturnData
	//微服务对象 全部连接列表
	ServiceObjConnList(name string) *zb.ReturnData
	//所有自动发现服务
	EtcdAll() *zb.ReturnData
}

func NewControl() Control {
	return &control{}
}

//rpc控制接口服务
func rpcServer(waitGroup *sync.WaitGroup) {
	defer waitGroup.Done()
	service := hprose.NewHTTPService()
	log.Info("启动RPC控制接口,端口:" + Config.Control.Port)
	con := NewControl()

	service.AddAllMethods(con)

	err := http.ListenAndServe(":"+Config.Control.Port, service)
	if err != nil {
		log.Error("启动RPC控制接口失败,端口:" + Config.Control.Port + "," + err.Error())
	}
}

//控制rpc接口对象定义
type control struct {
}

func (t *control) EtcdAll() *zb.ReturnData{
	if Config.Etcd.Switch{
		return zb.NewReturnData(0,"success",autoService.Keys())
	}
	return zb.NewReturnData(1003,"获取失败：自动发现服务关闭","")
}

func (t *control) MappingChange() *zb.ReturnData {
	go initMappingList()
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ManualChange() *zb.ReturnData {
	go initManualList()
	return zb.NewReturnData(0, "success", "")
}

func (t *control) EtcdServersChange() *zb.ReturnData {
	initEtcdServerList()
	if Config.Etcd.Switch {
		autoService, _ = etcd.NewMaster(etcdServers, "services/")
	}
	return zb.NewReturnData(0, "success", "")
}

func (t *control) MappingAdd(k string, v string) *zb.ReturnData {
	Mapping.Add(k, v)
	return zb.NewReturnData(0, "success", "")
}

func (t *control) MappingDelete(k string) *zb.ReturnData {
	Mapping.Delete(k)
	return zb.NewReturnData(0, "success", "")
}

func (t *control) MappingAllList() *zb.ReturnData {
	list := Mapping.All()
	return zb.NewReturnData(0, "success", list)
}

func (t *control) MappingGet(k string) *zb.ReturnData {
	value, err := Mapping.Get(k)
	if err != nil {
		return zb.NewReturnData(1001, "映射不存在", "")
	}
	return zb.NewReturnData(0, "success", map[string]string{"value": value})
}

func (t *control) ManualDelete(key string) *zb.ReturnData {
	manualList.Delete(key)
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ManualDeleteUrl(key string, uri string) *zb.ReturnData {
	err := manualList.DeleteUrl(key, uri)
	if err != nil {
		return zb.NewReturnData(1002, "删除失败："+err.Error(), "")
	}
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ManualGet(key string) *zb.ReturnData {
	list, err := manualList.Get(key)
	if err != nil {
		return zb.NewReturnData(1003, "获取失败："+err.Error(), "")
	}
	return zb.NewReturnData(0, "success", list)
}

func (t *control) ManualAddUrl(key string, uri string) *zb.ReturnData {
	err := manualList.AddUrl(key, uri)
	if err != nil {
		return zb.NewReturnData(1004, "增加失败："+err.Error(), "")
	}
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ManualAllList() *zb.ReturnData {
	list := manualList.All()
	return zb.NewReturnData(0, "success", list)
}

func (t *control) ManualAdd(key string, list []string) *zb.ReturnData {
	err := manualList.Add(key, list)
	if err != nil {
		return zb.NewReturnData(1004, "增加失败："+err.Error(), "")
	}
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ServicesAll() *zb.ReturnData {
	list := services.All()
	return zb.NewReturnData(0, "success", list)
}

func (t *control) ServiceDelete(name string) *zb.ReturnData {
	err := services.Delete(name)
	if err != nil {
		return zb.NewReturnData(1002, "删除失败："+err.Error(), "")
	}
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ServiceObjDeleteConn(name string, uri string) *zb.ReturnData {
	s, err := services.Get(name)
	if err != nil {
		return zb.NewReturnData(1002, "删除失败：获取服务对象失败："+err.Error(), "")
	}
	s.DeleteConn(uri)
	return zb.NewReturnData(0, "success", "")
}

func (t *control) ServiceObjConnCount(name string) *zb.ReturnData {
	s, err := services.Get(name)
	if err != nil {
		return zb.NewReturnData(1003, "获取失败：获取服务对象失败："+err.Error(), "")
	}
	num := s.ConnCount()
	return zb.NewReturnData(0, "success", map[string]int{"count": num})
}

func (t *control) ServiceObjConnList(name string) *zb.ReturnData {
	s, err := services.Get(name)
	if err != nil {
		return zb.NewReturnData(1003, "获取失败：获取服务对象失败："+err.Error(), "")
	}
	var result = make([]string, 0)
	l := s.ConnList()
	for k, _ := range l {
		result = append(result, k)
	}
	return zb.NewReturnData(0, "success", result)
}
